package com.longzz.util;


import com.longzz.bean.Danmu;
import lombok.extern.log4j.Log4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by longzzz on 2018/6/27.
 */
@Component
@Log4j
public class HttpUtil {

    @Autowired
    private DateUtil dateUtil;

    public List<Danmu> getDanmu(String url) throws Exception{
         List<Danmu> danmuList = new ArrayList<>();
         CloseableHttpClient closeableHttpClient = HttpClients.createDefault() ;
         //String aid = url.split("/video/av")[1].split("/")[0];
         HttpGet httpGet = new HttpGet(url) ;
//         httpGet.setHeader(new BasicHeader("Content-Type", "text/html; charset=utf-8"));
//         httpGet.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
         httpGet.setHeader(new BasicHeader("User-Agent", "'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36"));
         CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet) ;
         HttpEntity httpEntity = httpResponse.getEntity() ;
         String en= EntityUtils.toString(httpEntity,"UTF-8") ;
         log.info(en);
        //"cid=16496518&aid=9979006&pre_ad="
         String con = "cid=(.*)?&aid=" ;
         Pattern ah = Pattern.compile(con);
         Matcher mr = ah.matcher(en);
         while(mr.find()) {
                 String id = mr.group() ;
                 String newUrl = id.replace("cid=","") ;
                 String x = newUrl.replace("&aid=","") ;
                 log.info(x);
                 if(!x .equals("\" + player.cid + \"")){
                     HttpGet httpGet1 = new HttpGet("http://comment.bilibili.com/"+x+".xml");
                     CloseableHttpResponse httpResponse1 = closeableHttpClient.execute(httpGet1) ;
                     HttpEntity httpEntity1 = httpResponse1.getEntity() ;
                     String en1 = EntityUtils.toString(httpEntity1,"UTF-8") ;
                     //<d p="0.00000,1,25,16777215,1526607171,0,b5d1297c,4544165935">想知道是什么电影</d>
                     String c = "<d p=(.*?)</d>" ;
                     Pattern a = Pattern.compile(c);
                     Matcher m = a.matcher(en1);
                     while(m.find()){
                         log.info(m.group());
                         //"0.00000,1,25,16777215,1526607171,0,b5d1297c,4544165935">想知道是什么电影
                         String danmuTime = m.group().replace("<d p=\"","").replace("</d>","").split(",")[0];
                         String time = dateUtil.danmuFormateDate(danmuTime);
                         String content = m.group().split("\">")[1].replace("</d>","");
                         long sendDateTime = Long.parseLong(m.group().replace("<d p=\"","").replace("</d>","").split(",")[4]);
                         log.info(time);
                         log.info(content);
                         log.info(new Timestamp(sendDateTime * 1000));
                         Danmu danmu = new Danmu();
                         danmu.setTime(time);
                         danmu.setContent(content);
                         danmu.setSequence((int)Float.parseFloat(danmuTime));
                         danmu.setSendDateTime(new Timestamp(sendDateTime * 1000));
                         danmuList.add(danmu);
                     }
                 }
         }
        Collections.sort(danmuList, new Comparator<Danmu>() {
            public int compare(Danmu arg0, Danmu arg1) {
                int hits0 = arg0.getSequence();
                int hits1 = arg1.getSequence();
                if (hits1 < hits0) {
                    return 1;
                } else if (hits1 == hits0) {
                    return 0;
                } else {
                    return -1;
                }
            }
        });
        return  danmuList;
    }
}
